//
//  ManagerTaskViewController.m
//  MyProject
//
//  Created by Pei on 16/8/7.
//  Copyright © 2016年 Xiaobo Pei. All rights reserved.
//

#import "ManagerTaskViewController.h"

#import "HTHorizontalSelectionList.h"
#import "ScanViewController.h"
//#import "SelectProjectViewController.h"
#import "SelectCheckTypeViewController.h"
#import "SelectCheckItemViewController.h"
//#import "SelectModel.h"
#import "CheckItemCell.h"
//#import "CheckDetailViewController.h"
#import "UserInfo.h"
#import "CreateTaskSelectPersonViewController.h"
#import "ManagerTaskCheckDetailViewController.h"
#import "ScanPersonViewController.h"
#import "ScanCheckItemViewController.h"


@interface ManagerTaskViewController ()<HTHorizontalSelectionListDelegate, HTHorizontalSelectionListDataSource, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, SelectCheckTypeViewControllerDelegate, ScanViewControllerDelegate, SelectCheckItemViewControllerDelegate,ManagerTaskCheckDetailViewControllerDelegate,CreateTaskSelectPersonViewControllerDelegate,UIGestureRecognizerDelegate,ScanPersonViewControllerDelegate,ScanCheckItemViewControllerDelegate>
{
    HTHorizontalSelectionList *_segementView;
    
    UITableView *_myTable;
    UITableView *_secondMyTable;
    
    NSArray *_titleArray;
    
    UIView *_footView;
    
    NSDictionary *_checkDic;
    
    UIDatePicker *_datePicker;
    UIView *_dateTopView;
    
    NSString *_dateStr;
    
    UILabel *_detailLbl;
    
    UILabel *_detailLbl2;
    
    UILabel *_detailLbl3;
    
    NSArray *_secondData;
    
    NSString *_InspectionNoStr;
    
    UITextField *_remarkField;
    
    NSDictionary *_InspectOrgAndProjectDic;
    
    UIView *secondBottomView;
    
    NSDictionary *_currentDic;
    
    NSArray *peiTongPersonArray;
    
    UILabel *titleLbl3;
    
    NSDictionary *ScanCheckItemDic;

}

@end

@implementation ManagerTaskViewController

- (id)initWithData:(NSDictionary*)dic{
    self = [super init];
    if (self) {
        _currentDic = dic;
        
        _InspectOrgAndProjectDic = [_currentDic objectForKey:@"InspectOrgAndProject"];
        _checkDic = [_currentDic objectForKey:@"InspectType"];
        _dateStr = [_currentDic valueForKey:@"InspectDate"];
        _secondData = [_currentDic objectForKey:@"InspectionItems"];
        
        _InspectionNoStr = [_currentDic valueForKey:@"InspectionNo"];
        
        peiTongPersonArray = [_currentDic objectForKey:@"AccompanyPersonUIDsList"];
        
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [[NSNotificationCenter defaultCenter] postNotificationName:Hide_TabBar_Notification object:nil];
}

- (void)viewDidLoad {
    self.navTitle = @"检查单";
    [super viewDidLoad];
    

    
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 22)];
    [rightBtn setTitle:@"保存" forState:UIControlStateNormal];
    [rightBtn setTitleColor:kBlueColor forState:UIControlStateNormal];
    rightBtn.titleLabel.font = kAppFont(Home_Big_Size);
    [rightBtn addTarget:self action:@selector(rightBtnAcion) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:rightBtn]];
    
    _titleArray = @[@"检查单信息", @"检查项"];
    
    [self initSelectView];
    [self initTable];
    [self initFootView];
    
    [self initSecondMyTableView];
}



#pragma mark ********************** UI **************************
- (void)initSelectView{
    _segementView = [[HTHorizontalSelectionList alloc] initWithFrame:CGRectMake(0.0f, 0.0f, kDeviceWidth, 40.0f)];
    _segementView.delegate = self;
    _segementView.dataSource = self;
    _segementView.autoFillWidth = YES;
    _segementView.font = 14;
    _segementView.sideMargin = 0;
    _segementView.isAdjustBottomLine = YES;
    _segementView.bottomLineMargin = 10.f;
    _segementView.selectionIndicatorColor = kBlueColor;
    [_segementView setSelectedButtonIndex:0];
    [_segementView setTitleColor:kGreenColor forState:UIControlStateNormal];
    [_segementView setBottomTrimColor:kGreenColor];
    [self.view addSubview:_segementView];
    
}

- (void)initSecondMyTableView{
    _secondMyTable = [[UITableView alloc] init];
    _secondMyTable.frame = CGRectMake(0.0f, _segementView.bottom, kDeviceWidth, kDeviceHeight-NAV_HEIGHT-_segementView.height-kTabHeight);
    _secondMyTable.delegate = self;
    _secondMyTable.dataSource = self;
    _secondMyTable.backgroundColor = kRGB(241, 246, 250);
    _secondMyTable.tag = 119;
    [self.view addSubview:_secondMyTable];
    
    _secondMyTable.tableFooterView = [UIView new];
    
    secondBottomView = [[UIView alloc] init];
    secondBottomView.frame = CGRectMake(0.0f, _segementView.bottom+(kDeviceHeight-NAV_HEIGHT-_segementView.height-kTabHeight), kDeviceWidth, kTabHeight);
    [self.view addSubview:secondBottomView];
    
    //114 122 162
    UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancleBtn.frame = CGRectMake(0.0f, 0.0f, kDeviceWidth/2.0f, kTabHeight);
    [cancleBtn setBackgroundColor:kRGB(114, 122, 162)];
    [cancleBtn setTitle:@"选择" forState:UIControlStateNormal];
    [cancleBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
    cancleBtn.titleLabel.font = kAppFont(Home_Big_Size);
    cancleBtn.tag = 116;
    [cancleBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [secondBottomView addSubview:cancleBtn];
    
    UIButton *uploadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    uploadBtn.frame = CGRectMake(kDeviceWidth/2.0f, 0.0f, kDeviceWidth/2.0f, kTabHeight);
    [uploadBtn setBackgroundColor:kBtnBackgroundColor];
    [uploadBtn setTitle:@"扫码" forState:UIControlStateNormal];
    [uploadBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
    uploadBtn.titleLabel.font = kAppFont(Home_Big_Size);
    [uploadBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    uploadBtn.tag = 118;
    [secondBottomView addSubview:uploadBtn];
}

- (void)btnAction:(UIButton*)sender{
    switch (sender.tag) {
        case 116:
        {
            SelectCheckItemViewController *checkItemVC = [[SelectCheckItemViewController alloc] init];
            checkItemVC.delegate = self;
            [self.navigationController pushViewController:checkItemVC animated:YES];
        }
            break;
        case 118:
        {
            ScanCheckItemViewController *itemVC = [[ScanCheckItemViewController alloc] init];
            itemVC.delegate = self;
            [self.navigationController pushViewController:itemVC animated:YES];
        }
            break;
        default:
            break;
    }
}

- (void)passScanCheckItemValue:(NSDictionary *)tempDic{
    NSLog(@"====%@", tempDic);
    
    ScanCheckItemDic = tempDic;
    
    
    
    
    //Get_Check_Item_By_Major  InspectItemCode={Code}
    [[YMNetLib getInstance] connectWithAction:Get_Check_Item_By_Major_API method:@"GET" withContent:[NSString stringWithFormat:@"InspectItemCode=%@", [tempDic valueForKey:@"SpecialtyCode"]] withBody:nil withDelegate:self];//SpecialtyCode
}

- (void)NetDoOverSuccess:(NSDictionary*)body
{
    if ([[YMNetDataUtil getSuccessState:body] intValue] == 1)
    {
        //dataDic = body;
        
        //dataArray = [_dataDic objectForKey:@"InspectItemList"];
        
        //NSMutableArray *tempArray = [[NSMutableArray alloc] initWithArray:dataArray];
        //        NSMutableArray *tempArray = [[NSMutableArray alloc] initWithCapacity:3];
        //        [tempArray removeAllObjects];
        //        for (NSDictionary *tempD in dataArray) {
        //            NSMutableDictionary *ddd = [[NSMutableDictionary alloc] initWithDictionary:tempD];
        //            [ddd setValue:@"0" forKey:@"isSelect"];
        //            [tempArray addObject:ddd];
        //        }
        
        //dataArray = tempArray;
        NSDictionary *passDic;
        NSArray *InspectItemListAA = [body objectForKey:@"InspectItemList"];
        for (NSDictionary *aDDD in InspectItemListAA) {
            if ([[aDDD valueForKey:@"InspectItemID"] intValue] == [[ScanCheckItemDic valueForKey:@"InspectItemID"] intValue]) {
                passDic = aDDD;
            }
        }
        
        
        NSMutableDictionary *spaDic = [[NSMutableDictionary alloc] initWithDictionary:passDic];
        NSDictionary *kkkk = [[NSDictionary alloc] initWithObjectsAndKeys:[ScanCheckItemDic valueForKey:@"SpecialtyName"],@"SpecialtyName", nil];
        [spaDic setObject:kkkk forKey:@"superDic"];
        
        passDic = spaDic;
        
        NSLog(@"++++++++++++");
        NSMutableArray *mAry = [[NSMutableArray alloc] initWithArray:_secondData];
        
        BOOL isExist = NO;
        for (NSDictionary *YYYY in _secondData) {
            if([[passDic valueForKey:@"InspectItemID"] intValue] == [[YYYY valueForKey:@"InspectItemID"] intValue]){
                isExist = YES;
            }
            
            if (!isExist) {
                [mAry addObject:passDic];
            }
        }
        
        _secondData = mAry;
        [_secondMyTable reloadData];
        
    }
}

- (void)initTable{
    _myTable = [[UITableView alloc] init];
    _myTable.frame = CGRectMake(0.0f, _segementView.bottom, kDeviceWidth, kDeviceHeight-NAV_HEIGHT-_segementView.height);
    _myTable.delegate = self;
    _myTable.dataSource = self;
    _myTable.backgroundColor = kRGB(241, 246, 250);
    _myTable.tag = 116;
    [self.view addSubview:_myTable];
}
- (void)initFootView{
    _footView = [[UIView alloc] init];
    _footView.frame = CGRectMake(0.0f, 0.0f, kDeviceWidth, 54*4+40);
    _footView.backgroundColor = kWhiteColor;
    _myTable.tableFooterView = _footView;
    
    UIControl *companyControl = [[UIControl alloc] init];
    companyControl.frame = CGRectMake(0.0f, 0.0f, kDeviceWidth, 54.0f);
    [_footView addSubview:companyControl];
    
    UIImageView *arrowImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"02jiantou-"]];
    arrowImageView.frame = CGRectMake(kDeviceWidth-60.0f-10.0f/2.0f, (companyControl.height-19.0f/2.0f)/2.0f, 10.0f/2.0f, 19.0f/2.0f);
    [companyControl addSubview:arrowImageView];
    
    UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(16.0f, (companyControl.height-(Home_Small_Size+2.0f))/2.0f, 60.0f, Home_Small_Size+2.0f)];
    titleLbl.textColor = kRGB(205, 206, 207);
    titleLbl.text = @"检查公司";
    titleLbl.font = kAppFont(Home_Small_Size+2.0f);
    [companyControl addSubview:titleLbl];
    
    _detailLbl = [[UILabel alloc] initWithFrame:CGRectMake(titleLbl.right, (companyControl.height-(Home_Small_Size))/2.0f, kDeviceWidth-16.0f-60.0f-arrowImageView.width-titleLbl.width-20.0f, Home_Small_Size)];
    _detailLbl.textColor = kRGB(100, 99, 104);
    _detailLbl.text = [_InspectOrgAndProjectDic valueForKey:@"unitName"];
    _detailLbl.font = kAppFont(Home_Small_Size);
    _detailLbl.textAlignment = NSTextAlignmentRight;
    [companyControl addSubview:_detailLbl];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(16.0f, companyControl.height-0.5f, kDeviceWidth-16.0f-60.0f, 0.5f)];
    lineView.backgroundColor = kGreenColor;
    [companyControl addSubview:lineView];
    
    UIControl *projectControl = [[UIControl alloc] init];
    projectControl.frame = CGRectMake(0.0f, companyControl.bottom, kDeviceWidth, 54.0f);
    [projectControl addTarget:self action:@selector(projectControlAction) forControlEvents:UIControlEventTouchUpInside];
    [_footView addSubview:projectControl];
    
    UIImageView *arrowImageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"02jiantou-"]];
    arrowImageView2.frame = CGRectMake(kDeviceWidth-60.0f-10.0f/2.0f, (projectControl.height-19.0f/2.0f)/2.0f, 10.0f/2.0f, 19.0f/2.0f);
    [projectControl addSubview:arrowImageView2];
    
    UILabel *titleLbl2 = [[UILabel alloc] initWithFrame:CGRectMake(16.0f, (projectControl.height-(Home_Small_Size+2.0f))/2.0f, 60.0f, Home_Small_Size+2.0f)];
    titleLbl2.textColor = kRGB(205, 206, 207);
    titleLbl2.text = @"检查项目";
    titleLbl2.font = kAppFont(Home_Small_Size+2.0f);
    [projectControl addSubview:titleLbl2];
    
    _detailLbl2 = [[UILabel alloc] initWithFrame:CGRectMake(titleLbl2.right, (projectControl.height-(Home_Small_Size))/2.0f, kDeviceWidth-16.0f-60.0f-arrowImageView2.width-titleLbl2.width-20.0f, Home_Small_Size)];
    _detailLbl2.textColor = kRGB(100, 99, 104);
    _detailLbl2.text = [_InspectOrgAndProjectDic valueForKey:@"projectName"];
    _detailLbl2.font = kAppFont(Home_Small_Size);
    _detailLbl2.textAlignment = NSTextAlignmentRight;
    [projectControl addSubview:_detailLbl2];
    
    UIView *lineView2 = [[UIView alloc] initWithFrame:CGRectMake(16.0f, projectControl.height-0.5f, kDeviceWidth-16.0f, 0.5f)];
    lineView2.backgroundColor = kGreenColor;
    [projectControl addSubview:lineView2];
    
    
    UIControl *personControl = [[UIControl alloc] init];
    personControl.frame = CGRectMake(0.0f, projectControl.bottom, kDeviceWidth, 54.0f);
    //[personControl addTarget:self action:@selector(personControlAction) forControlEvents:UIControlEventTouchUpInside];
    [_footView addSubview:personControl];
    
    //UIImageView *arrowImageView3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"02jiantou-"]];
    //arrowImageView3.frame = CGRectMake(kDeviceWidth-60.0f-10.0f/2.0f, (personControl.height-19.0f/2.0f)/2.0f, 10.0f/2.0f, 19.0f/2.0f);
    //[personControl addSubview:arrowImageView3];
    
    titleLbl3 = [[UILabel alloc] initWithFrame:CGRectMake(16.0f, (personControl.height-(Home_Small_Size+2.0f))/2.0f, kDeviceWidth-16.0f*2.0f-63-16, Home_Small_Size+2.0f)];
    titleLbl3.textColor = kRGB(205, 206, 207);
    //titleLbl3.text = @"检查人";
    
    NSMutableString *personStr = [[NSMutableString alloc] initWithCapacity:3];
    for (NSDictionary *nary in peiTongPersonArray) {
        [personStr appendString:[NSString stringWithFormat:@"%@ ",[nary valueForKey:@"UserName"]]];
    }
    
    titleLbl3.text = [NSString stringWithFormat:@"检查人:%@", personStr];
    
    titleLbl3.font = kAppFont(Home_Small_Size+2.0f);
    [personControl addSubview:titleLbl3];
    
    
    UIButton *personScanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [personScanBtn setBackgroundImage:[UIImage imageNamed:@"01saoma"] forState:UIControlStateNormal];
    personScanBtn.frame = CGRectMake(kDeviceWidth-16.0f-62.0f/2.0f, (54-62/2)/2.0f, 63.0f/2.0f, 62.0f/2.0f);
    [personScanBtn addTarget:self action:@selector(personScanBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [personControl addSubview:personScanBtn];
    
    UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [addBtn setBackgroundImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
    addBtn.frame = CGRectMake(kDeviceWidth-16.0f-62.0f/2.0f-10-62/2.0f, (54-62/2)/2.0f, 63.0f/2.0f, 62.0f/2.0f);
    [addBtn addTarget:self action:@selector(addBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [personControl addSubview:addBtn];
    
    _detailLbl3 = [[UILabel alloc] initWithFrame:CGRectMake(titleLbl3.right, (personControl.height-(Home_Small_Size))/2.0f, kDeviceWidth-16.0f-titleLbl3.width-16-personScanBtn.width-10.0f-addBtn.width, Home_Small_Size)];
    _detailLbl3.textColor = kRGB(100, 99, 104);
    _detailLbl3.text = @"";
    _detailLbl3.font = kAppFont(Home_Small_Size);
    _detailLbl3.textAlignment = NSTextAlignmentRight;
    [personControl addSubview:_detailLbl3];
    
    
    //    UIButton *personScanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //    [personScanBtn setBackgroundImage:[UIImage imageNamed:@"01saoma"] forState:UIControlStateNormal];
    //    personScanBtn.frame = CGRectMake(kDeviceWidth-16.0f-63.0f/2.0f, (54-62/2)/2.0f, 63.0f/2.0f, 62.0f/2.0f);
    //    [personScanBtn addTarget:self action:@selector(personScanBtnAction) forControlEvents:UIControlEventTouchUpInside];
    //    [personControl addSubview:personScanBtn];
    
    UIView *lineView3 = [[UIView alloc] initWithFrame:CGRectMake(16.0f, personControl.height-0.5f, kDeviceWidth-16.0f, 0.5f)];
    lineView3.backgroundColor = kGreenColor;
    [personControl addSubview:lineView3];
    
    
    
    UILabel *remarkLbl = [[UILabel alloc] initWithFrame:CGRectMake(16.0f, personControl.bottom+10.0f, 46.0f, Home_Small_Size+2.0f)];
    remarkLbl.textColor = kRGB(205, 206, 207);
    remarkLbl.text = @"备注：";
    remarkLbl.font = kAppFont(Home_Small_Size+2.0f);
    [_footView addSubview:remarkLbl];
    
    _remarkField = [[UITextField alloc] init];
    _remarkField.frame = CGRectMake(remarkLbl.right+5.0f, remarkLbl.origin.y, kDeviceWidth-16.0f*2.0f-remarkLbl.width, 74.0f);
    _remarkField.attributedPlaceholder =[[NSAttributedString alloc] initWithString:@"再次输入需要备注的内容，不超过100字" attributes:@{NSForegroundColorAttributeName: kGreenColor, NSFontAttributeName : kAppFont(Home_Small_Size)}];
    _remarkField.font = kAppFont(Home_Small_Size);
    _remarkField.delegate = self;
    _remarkField.text = [_currentDic valueForKey:@"InspectionRemark"];
    _remarkField.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
    [_footView addSubview:_remarkField];
    
    UIView *lineView4 = [[UIView alloc] initWithFrame:CGRectMake(0.0f, _footView.height-0.5f, kDeviceWidth, 0.5f)];
    lineView4.backgroundColor = kGreenColor;
    [_footView addSubview:lineView4];
    
    UIButton *scanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [scanBtn setBackgroundImage:[UIImage imageNamed:@"01saoma"] forState:UIControlStateNormal];
    scanBtn.frame = CGRectMake(kDeviceWidth-16.0f-63.0f/2.0f, (54.0f*2.0f-62.0f/2.0f)/2.0f, 63.0f/2.0f, 62.0f/2.0f);
    [scanBtn addTarget:self action:@selector(scanBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [_footView addSubview:scanBtn];
    
}
#pragma mark ********************** Action *******************************
- (void)personScanBtnAction{
    ScanPersonViewController *personVC = [[ScanPersonViewController alloc] init];
    personVC.delegate = self;
    [self.navigationController pushViewController:personVC animated:YES];
}

- (void)passScanPersonValue:(NSArray *)tempData{    
    NSLog(@"=====%@", tempData);
    
    if (peiTongPersonArray != nil && peiTongPersonArray.count > 0) {
        NSMutableArray *tempAry = [[NSMutableArray alloc] initWithArray:peiTongPersonArray];
        if (tempData != nil && tempData.count > 0) {
            for (NSDictionary *personDic in tempData) {
                BOOL isExit = NO;
                for (NSDictionary *tempD in peiTongPersonArray) {
                    if ([[personDic valueForKey:@"UserID"] isEqualToString:[tempD valueForKey:@"UserID"]]) {
                        isExit = YES;
                    }
                }
                
                if (!isExit) {
                    [tempAry addObject:personDic];
                }
            }
            
            peiTongPersonArray = tempAry;
        }
    }else{
        peiTongPersonArray = tempData;
    }
    
    //peiTongPersonArray = tempData;
    
    NSMutableString *personStr = [[NSMutableString alloc] initWithCapacity:3];
    for (NSDictionary *nary in peiTongPersonArray) {
        [personStr appendString:[NSString stringWithFormat:@"%@ ",[nary valueForKey:@"userName"]]];
    }
    
    titleLbl3.text = [NSString stringWithFormat:@"检查人:%@", personStr];

}

- (void)addBtnAction{
    CreateTaskSelectPersonViewController *personVC = [[CreateTaskSelectPersonViewController alloc] initWithData:peiTongPersonArray super:nil];
    personVC.delegate = self;
    [self.navigationController pushViewController:personVC animated:YES];
}

- (void)passData:(NSArray *)ary superDic:(NSDictionary *)superDic yuanPerson:(NSArray *)personA{
    
    if (ary == nil || ary.count <= 0) {
        peiTongPersonArray = @[];
    }

    
    if (personA == nil || personA.count <= 0) {
        if (peiTongPersonArray != nil && peiTongPersonArray.count > 0) {
            NSMutableArray *tempAry = [[NSMutableArray alloc] initWithArray:peiTongPersonArray];
            if (ary != nil && ary.count > 0) {
                for (NSDictionary *personDic in ary) {
                    BOOL isExit = NO;
                    for (NSDictionary *tempD in peiTongPersonArray) {
                        if ([[personDic valueForKey:@"UserID"] isEqualToString:[tempD valueForKey:@"UserID"]]) {
                            isExit = YES;
                        }
                    }
                    
                    if (!isExit) {
                        [tempAry addObject:personDic];
                    }
                }
                
                peiTongPersonArray = tempAry;
            }
        }else{
            peiTongPersonArray = ary;
        }
    }else{
        
        if (peiTongPersonArray != nil && peiTongPersonArray.count > 0) {
            if (ary != nil && ary.count > 0) {
                for (int m = 0; m < personA.count; m ++) {
                    NSDictionary *TTTT = [personA objectAtIndex:m];
                    BOOL isExit = NO;
                    for (NSDictionary *nnn in ary) {
                        if ([[nnn valueForKey:@"UserID"] isEqualToString:[TTTT valueForKey:@"UserID"]]) {
                            isExit = YES;
                        }
                    }
                    
                    NSMutableArray *BBBBBB = [[NSMutableArray alloc] initWithArray:peiTongPersonArray];
                    if (!isExit) {
                        for (NSDictionary *uuuu in peiTongPersonArray) {
                            if ([[uuuu valueForKey:@"UserID"] isEqualToString:[TTTT valueForKey:@"UserID"]]) {
                                [BBBBBB removeObject:uuuu];
                            }
                        }
                    }
                    peiTongPersonArray = BBBBBB;
                    
                }
            }
        }
        
        
        
    }
    
    
    
    NSMutableString *personStr = [[NSMutableString alloc] initWithCapacity:3];
    for (NSDictionary *nary in peiTongPersonArray) {
        [personStr appendString:[NSString stringWithFormat:@"%@ ",[nary valueForKey:@"UserName"]]];
    }
    
    titleLbl3.text = [NSString stringWithFormat:@"检查人:%@", personStr];

}


- (void)rightBtnAcion
{
    
    NSMutableArray *contentArray = nil;
    NSArray *tempA = (NSArray*)[[NSUserDefaults standardUserDefaults] objectForKey:Create_Task];
    if (tempA != nil && tempA.count > 0)
    {
        contentArray = [[NSMutableArray alloc] initWithArray:tempA];
    }
    
    
    
    if (_checkDic == nil || _checkDic.count <= 0){
        [[YMUIUtil getInstance] showTipViewAutoDis:@"检查类型不能为空"];
        return;
    }
    //_InspectOrgAndProjectDic
    if (_InspectOrgAndProjectDic == nil || _InspectOrgAndProjectDic.count <= 0){
        [[YMUIUtil getInstance] showTipViewAutoDis:@"检查公司和项目不能为空"];
        return;
    }
    
    if (_dateStr == nil || _dateStr.length <= 0){
        [[YMUIUtil getInstance] showTipViewAutoDis:@"检查日期不能为空"];
        return;
    }
    
//    if (_secondData == nil || _secondData.count <= 0) {
//        [[YMUIUtil getInstance] showTipViewAutoDis:@"检查项不能为空"];
//        return;
//    }
    
    NSString *remarkStr = _remarkField.text;
    if (remarkStr == nil || [remarkStr isEqualToString:@""]) {
        [[YMUIUtil getInstance] showTipViewAutoDis:@"检查备注不能为空"];
        return;
    }
    //    NSString *str = _remarkField.text;
    //    if (str == nil || str.length <= 0) {
    //        [[YMUIUtil getInstance] showTipViewAutoDis:@"检查单备注不能为空"];
    //        return;
    //    }
    
    NSArray *tempAY;
    if (peiTongPersonArray == nil || peiTongPersonArray.count <= 0) {
        tempAY = @[];
    }else{
        tempAY = peiTongPersonArray;
    }

    NSDictionary *bodyDic = [NSDictionary dictionaryWithObjectsAndKeys:
                             [[UserInfo getInstance] getUserID],@"UserID",//UserID
                             _InspectionNoStr,@"InspectionNo", //检查单号
                             [NSNumber numberWithBool:NO], @"isSelect",
                             _checkDic, @"InspectType", //检查类型 int
                             _InspectOrgAndProjectDic, @"InspectOrgAndProject", //被检查公司,项目
                             _dateStr, @"InspectDate", //检查时间 DateTime
                             @"0", @"InspectionState", //检查单状态 int
                             _remarkField.text, @"InspectionRemark", //检查单备注
                             //_secondData,@"InspectionItems",
                             tempAY,@"AccompanyPersonUIDsList",
                             _secondData,@"InspectionItems",
                             nil];
    

    for (int m = 0; m < contentArray.count; m ++) {
        NSDictionary *DDD = [contentArray objectAtIndex:m];
        if ([[DDD valueForKey:@"InspectionNo"] isEqualToString:_InspectionNoStr]) {
            [contentArray replaceObjectAtIndex:m withObject:bodyDic];
        }
    }
    
    //[contentArray addObject:bodyDic];
    
    [[NSUserDefaults standardUserDefaults] setObject:contentArray forKey:Create_Task];
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)scanBtnAction{
    ScanViewController *scanVC = [[ScanViewController  alloc] init];
    scanVC.delegate = self;
    [self.navigationController pushViewController:scanVC animated:YES];
}

- (void)projectControlAction
{
    //SelectProjectViewController *selectVC = [[SelectProjectViewController alloc] init];
    //selectVC.navTitle = @"选择检查项";
    //[self.navigationController pushViewController:selectVC animated:YES];
}

- (void)sureBtnAcion
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    // 格式化日期格式
    formatter. dateFormat = @"yyyy-MM-dd" ;
    _dateStr = [formatter stringFromDate:_datePicker.date];
    NSLog(@"=====%@", _dateStr);
    
    
    [_datePicker removeFromSuperview];
    _datePicker = nil;
    [_dateTopView removeFromSuperview];
    _dateTopView = nil;
    
    [_myTable reloadData];
}

- (void)cancleBtnAcion
{
    [_datePicker removeFromSuperview];
    _datePicker = nil;
    [_dateTopView removeFromSuperview];
    _dateTopView = nil;
}

#pragma mark ********************** Delegate ***************************
#pragma mark  ************************* UITableViewDataSource **************************************
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView.tag == 116) {
        return 3;
    }else{
        if (_secondData == nil || _secondData.count <= 0) {
            return 0;
        }
        return _secondData.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 116) {
        static NSString *identifier = @"cell_house_History";
        UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        }
        cell.textLabel.textColor = kRGB(205, 206, 207);
        cell.textLabel.font = kAppFont(Home_Small_Size+2);
        cell.detailTextLabel.font = kAppFont(Home_Small_Size);
        cell.detailTextLabel.textColor = kRGB(100, 99, 104);
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        switch (indexPath.row) {
            case 0:
            {
                cell.accessoryType = UITableViewCellAccessoryNone;
                cell.textLabel.text = @"检查单号";
                //JDC +yyyyMMddHHmmssSSS
                //NSDateFormatter *formatter =[[NSDateFormatter alloc] init];
                //[formatter setDateFormat:@"yyyyMMddHHmmssSSS"];
                //NSString *currentTime = [formatter stringFromDate:[NSDate date]];
                
                cell.detailTextLabel.text = [_currentDic valueForKey:@"InspectionNo"];
                
                //_InspectionNoStr = [NSString stringWithFormat:@"JDC%@", currentTime];
            }
                break;
            case 1:
            {
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                cell.textLabel.text = @"检查类型";
                if (_checkDic) {
                    cell.detailTextLabel.text = [_checkDic valueForKey:@"Name"];
                }
                
                
                UIImageView *arrowImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"02jiantou-"]];
                arrowImageView.frame = CGRectMake(kDeviceWidth-16.0f-10.0f/2.0f, (54.0f-19.0f/2.0f)/2.0f, 10.0f/2.0f, 19.0f/2.0f);
                
                cell.accessoryView = arrowImageView;
            }
                break;
            case 2:
            {
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                cell.textLabel.text = @"检查日期";
                cell.detailTextLabel.text = _dateStr;
                
                UIImageView *arrowImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"02jiantou-"]];
                arrowImageView.frame = CGRectMake(kDeviceWidth-16.0f-10.0f/2.0f, (54.0f-19.0f/2.0f)/2.0f, 10.0f/2.0f, 19.0f/2.0f);
                
                cell.accessoryView = arrowImageView;
                
                UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(16.0f, 53.5f, kDeviceWidth-16.0f, 0.5f)];
                lineView.backgroundColor = kGreenColor;
                [cell addSubview:lineView];
            }
                break;
            default:
                break;
        }
        return cell;
    }else{
        static NSString *identifier = @"StatisticsCell";
        CheckItemCell *cell = (CheckItemCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil)
        {
            cell = [[CheckItemCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        //SelectModel *selectModel = [_secondData objectAtIndex:indexPath.row];
        
        /*
         
         {
         InspectContentList =     (
         {
         InspectContentID = 3;
         InspectContentName = "\U65bd\U5de5\U673a\U68b0\U4e0d\U7b26\U8981\U6c42";
         MaxPoints = 10;
         RiskType = 3;
         RiskTypeName = "\U4e2a\U4eba\U9632\U62a4";
         },
         {
         InspectContentID = 4;
         InspectContentName = "\U65bd\U5de5\U673a\U68b0\U4f5c\U4e1a\U4f4d\U7f6e\U9009\U62e9\U4e0d\U5f53";
         MaxPoints = 10;
         RiskType = 3;
         RiskTypeName = "\U4e2a\U4eba\U9632\U62a4";
         },
         {
         InspectContentID = 5;
         InspectContentName = "\U65e0\U8bc1\U64cd\U4f5c";
         MaxPoints = 10;
         RiskType = 5;
         RiskTypeName = "\U6d88\U9632\U8bbe\U5907";
         },
         {
         InspectContentID = 6;
         InspectContentName = "\U65bd\U5de5\U4eba\U5458\U4eba\U5458\U8fdd\U7ae0\U4f5c\U4e1a";
         MaxPoints = 10;
         RiskType = 5;
         RiskTypeName = "\U6d88\U9632\U8bbe\U5907";
         },
         {
         InspectContentID = 7;
         InspectContentName = "\U65e0\U5173\U4eba\U5458\U8fdb\U5165\U65bd\U5de5\U73b0\U573a";
         MaxPoints = 10;
         RiskType = 0;
         RiskTypeName = "\U5b89\U5168\U7ba1\U7406";
         }
         );
         InspectItemCode = "DJYJCGC_TFKW";
         InspectItemID = 3;
         InspectItemName = "\U571f\U65b9\U5f00\U6316";
         InspectType = 0;
         isSelect = 1;
         superDic =     {
         SpecialtyCode = DJYJCGC;
         SpecialtyName = "\U5730\U57fa\U4e0e\U57fa\U7840\U5de5\U7a0b";
         };
         }
         
         */
        
        
        NSDictionary *tempD = [_secondData objectAtIndex:indexPath.row];
        
        
        NSArray *attachAry = [tempD objectForKey:@"InspectContentList"];
        
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.text = [tempD valueForKey:@"InspectItemName"];
        cell.textLabel.font = kAppFont(Home_Small_Size+2.0f);
        cell.textLabel.textColor = kBlackColor;
        
        cell.detailTextLabel.font = kAppFont(Home_Small_Size);
        cell.detailTextLabel.textColor = kGreenColor;
        
        BOOL isHas = NO;
        for (NSDictionary *attachDic in attachAry) {
            NSDictionary *RiskLevel_Dic;
            if ([attachDic objectForKey:@"RiskLevel_Dic"] != nil) {
                RiskLevel_Dic = [attachDic objectForKey:@"RiskLevel_Dic"];
            }else{
                RiskLevel_Dic = nil;
            }
            
            NSArray *imageAry;
            if ([attachDic objectForKey:@"ImageData_List"] != nil) {
                imageAry = [attachDic objectForKey:@"ImageData_List"];
            }else{
                imageAry = nil;
            }
            NSString *miaoShuStr = [attachDic valueForKey:@"JianChaMiaoShu"];
            if ([attachDic valueForKey:@"JianChaMiaoShu"] != nil && miaoShuStr.length > 0) {
                miaoShuStr = [attachDic valueForKey:@"JianChaMiaoShu"];
            }else{
                miaoShuStr = @"";
            }
            
            
            if (([attachDic valueForKey:@"KouFen"] != nil && [[attachDic valueForKey:@"KouFen"] intValue] > 0) && ([attachDic objectForKey:@"RiskLevel_Dic"] != nil && RiskLevel_Dic.count >0) && (imageAry != nil && imageAry.count >0)  && (miaoShuStr != nil && miaoShuStr.length > 0) ) {
                isHas = YES;
            }
        }
        
        //        NSDictionary *RiskLevel_Dic;
        //        if ([tempD objectForKey:@"RiskLevel_Dic"] != nil) {
        //            RiskLevel_Dic = [tempD objectForKey:@"RiskLevel_Dic"];
        //        }else{
        //            RiskLevel_Dic = nil;
        //        }
        //
        //        NSArray *imageAry;
        //        if ([tempD objectForKey:@"ImageData_List"] != nil) {
        //            imageAry = [tempD objectForKey:@"ImageData_List"];
        //        }else{
        //            imageAry = nil;
        //        }
        
        //        NSDictionary *tempSuperDic = [tempD objectForKey:@"superDic"];
        //        if (([tempD valueForKey:@"KouFen"] != nil && [[tempD valueForKey:@"KouFen"] intValue] > 0) && ([tempD objectForKey:@"RiskLevel_Dic"] != nil && RiskLevel_Dic.count >0) && (imageAry != nil && imageAry.count >0)) {
        //            cell.detailTextLabel.text = [tempSuperDic valueForKey:@"SpecialtyName"];
        //        }else{
        //            NSString *str = [NSString stringWithFormat:@"%@ [%@]", [tempSuperDic valueForKey:@"SpecialtyName"], @"信息不完整"];
        //            NSString *firstStr = [tempSuperDic valueForKey:@"SpecialtyName"];
        //            NSString *secondStr = [NSString stringWithFormat:@" [%@]", @"信息不完整"];
        //            NSMutableAttributedString *kouMStr = [[NSMutableAttributedString alloc] initWithString:str];
        //            [kouMStr addAttribute:NSForegroundColorAttributeName value:kRedColor range:NSMakeRange(firstStr.length,secondStr.length)];
        //            cell.detailTextLabel.attributedText = kouMStr;
        //
        //        }
        
        
        NSDictionary *tempSuperDic = [tempD objectForKey:@"superDic"];
        if (isHas) {
            cell.detailTextLabel.text = [tempSuperDic valueForKey:@"SpecialtyName"];
        }else{
            NSString *str = [NSString stringWithFormat:@"%@ [%@]", [tempSuperDic valueForKey:@"SpecialtyName"], @"信息不完整"];
            NSString *firstStr = [tempSuperDic valueForKey:@"SpecialtyName"];
            NSString *secondStr = [NSString stringWithFormat:@" [%@]", @"信息不完整"];
            NSMutableAttributedString *kouMStr = [[NSMutableAttributedString alloc] initWithString:str];
            [kouMStr addAttribute:NSForegroundColorAttributeName value:kRedColor range:NSMakeRange(firstStr.length,secondStr.length)];
            cell.detailTextLabel.attributedText = kouMStr;
            
        }
        
        
        
        [cell.deleteBtn addTarget:self action:@selector(deleteBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        cell.deleteBtn.tag = indexPath.row;
        
        return cell;
    }
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 116) {
        return 54.0f;
    }else{
        return 50.0f;
    }
    return 0.0f;
}


#pragma mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 116) {
        switch (indexPath.row) {
            case 1:
            {
                SelectCheckTypeViewController *checkVC = [[SelectCheckTypeViewController alloc] init];
                checkVC.delegate = self;
                [self.navigationController pushViewController:checkVC animated:YES];
            }
                break;
            case 2:
            {
                [_datePicker removeFromSuperview];
                _datePicker = nil;
                _datePicker = [[UIDatePicker alloc] init];
                _datePicker.datePickerMode = UIDatePickerModeDate;
                NSDate *afterDate = [NSDate dateWithTimeIntervalSinceNow:3*24*60*60];
                _datePicker.minimumDate = afterDate;//[NSDate date];
                //_datePicker.minimumDate = [NSDate date];
                _datePicker.frame = CGRectMake(0.0f, self.view.height-200.0f, kDeviceWidth, 200.0f);
                _datePicker.backgroundColor = kDefaultBackgroundColor;
                [self.view addSubview:_datePicker];
                
                [_dateTopView removeFromSuperview];
                _dateTopView = nil;
                _dateTopView = [[UIView alloc] init];
                _dateTopView.backgroundColor = kWhiteColor;
                _dateTopView.frame = CGRectMake(-0.5f, self.view.height-_datePicker.height-40.0f, kDeviceWidth+1.0f, 40.0f);
                _dateTopView.layer.borderColor = kGreenColor.CGColor;
                _dateTopView.layer.borderWidth = 0.5f;
                [self.view addSubview:_dateTopView];
                
                UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
                [sureBtn setTitle:@"确认" forState:UIControlStateNormal];
                [sureBtn setTitleColor:kBlueColor forState:UIControlStateNormal];
                sureBtn.titleLabel.font = kAppFont(Home_Big_Size);
                sureBtn.frame = CGRectMake(kDeviceWidth-40.0f-16.0f, (40-Home_Big_Size)/2.0f, 40.0f, Home_Big_Size);
                [sureBtn addTarget:self action:@selector(sureBtnAcion) forControlEvents:UIControlEventTouchUpInside];
                [_dateTopView addSubview:sureBtn];
                
                UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
                [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
                [cancleBtn setTitleColor:kBlueColor forState:UIControlStateNormal];
                cancleBtn.titleLabel.font = kAppFont(Home_Big_Size);
                cancleBtn.frame = CGRectMake(16.0f, (40-Home_Big_Size)/2.0f, 40.0f, Home_Big_Size);
                [cancleBtn addTarget:self action:@selector(cancleBtnAcion) forControlEvents:UIControlEventTouchUpInside];
                [_dateTopView addSubview:cancleBtn];
                
            }
                break;
            default:
                break;
        }
    }else{
        //SelectModel *selectModel = [_secondData objectAtIndex:indexPath.row];
        NSDictionary *selectModel = [_secondData objectAtIndex:indexPath.row];
        //CheckDetailViewController *checkDetailVC = [[CheckDetailViewController alloc] initWithInspectContentID:selectModel.InspectContentID];
        ManagerTaskCheckDetailViewController *checkDetailVC = [[ManagerTaskCheckDetailViewController alloc] initWithModel:selectModel projectAndCompany:_InspectOrgAndProjectDic checkType:_checkDic];
        checkDetailVC.delegate = self;
        [self.navigationController pushViewController:checkDetailVC animated:YES];
    }
    
}

#pragma mark - HTHorizontalSelectionList delegate
- (NSInteger)numberOfItemsInSelectionList:(HTHorizontalSelectionList *)selectionList
{
    return _titleArray.count;
}

- (NSString *)selectionList:(HTHorizontalSelectionList *)selectionList titleForItemWithIndex:(NSInteger)index
{
    return _titleArray[index];
}

- (void)selectionList:(HTHorizontalSelectionList *)selectionList didSelectButtonWithIndex:(NSInteger)index
{
    switch (index) {
        case 0:
        {
            [_secondMyTable removeFromSuperview];
            [secondBottomView removeFromSuperview];
            
            [self.view addSubview:_myTable];
            [_myTable reloadData];
        }
            break;
        case 1:
        {
            [_myTable removeFromSuperview];
            [self.view addSubview:_secondMyTable];
            [self.view addSubview:secondBottomView];
            [_secondMyTable reloadData];
        }
            break;
            
        default:
            break;
    }
}


- (void)deleteBtnAction:(UIButton*)sender
{
    NSDictionary *selectD = [_secondData objectAtIndex:sender.tag];
    NSMutableArray *tempArray = [[NSMutableArray alloc] initWithArray:_secondData];
    //    NSMutableArray *tempA = [[NSMutableArray alloc] initWithCapacity:3];
    //    for (SelectModel *tempM in tempArray) {
    //        if ([tempM isEqual:selectModel]) {
    //            [tempArray removeObject:tempM];
    //        }
    //    }
    
    for (int i=0; i<_secondData.count; i++) {
        NSDictionary *tempModel1 = [_secondData objectAtIndex:sender.tag];
        if ([[selectD valueForKey:@"InspectItemID"] intValue] == [[tempModel1 valueForKey:@"InspectItemID"] intValue]) {
            [tempArray removeObject:tempModel1];
        }
    }
    
    _secondData = tempArray;
    [_secondMyTable reloadData];
}

//选择检查项
- (void)passSelectModel:(NSArray *)tempArray
{
    NSLog(@"++++++++++++");
    NSMutableArray *mAry = [[NSMutableArray alloc] initWithArray:_secondData];
    
    for (int k = 0; k < tempArray.count; k ++) {
        BOOL isExist = NO;
        NSDictionary *bAry = [tempArray objectAtIndex:k];
        for (NSDictionary *YYYY in _secondData) {
            if([[bAry valueForKey:@"InspectItemID"] intValue] == [[YYYY valueForKey:@"InspectItemID"] intValue]){
                isExist = YES;
            }
        }
        
        if (!isExist) {
            [mAry addObject:bAry];
        }
    }
    
    _secondData = mAry;
    [_secondMyTable reloadData];
}

- (void)passScanValue:(NSDictionary *)tempDic
{
    _InspectOrgAndProjectDic = tempDic;
    
    _detailLbl.text = [tempDic valueForKey:@"unitName"];
    _detailLbl2.text = [tempDic valueForKey:@"projectName"];
}

- (void)passSelectCheckType:(NSDictionary *)tempDic
{
    NSLog(@"+++++++++++++++%@", [tempDic description]);
    _checkDic = tempDic;
    [_myTable reloadData];
}

- (void)passCheckDetailDic:(NSDictionary *)model
//- (void)passModel:(SelectModel *)model
{
    NSMutableArray *tempSecondArray = [[NSMutableArray alloc] initWithArray:_secondData];
    
    for (int i = 0; i < _secondData.count; i ++) {
        NSDictionary *selectModel = [_secondData objectAtIndex:i];
        if ([[model valueForKey:@"InspectItemID"] intValue] == [[selectModel valueForKey:@"InspectItemID"] intValue]) {
            [tempSecondArray replaceObjectAtIndex:i withObject:model];
        }
    }
    
    _secondData = tempSecondArray;
    [_secondMyTable reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
